﻿Imports System.Management

Public Class GetMappedDrives
    Public Shared Function GetMappedDrives(ByVal strcompname As String) As List(Of String)
        Dim wmiScope As ManagementScope
        Dim wmiConnectionOptions As New ConnectionOptions
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As ObjectQuery
        With wmiConnectionOptions
            .Impersonation = ImpersonationLevel.Impersonate
            .Authentication = AuthenticationLevel.Packet
        End With
        Dim mappeddrvs As List(Of String) = New List(Of String)
        Try
            wmiScope = New ManagementScope("\\" & strcompname & "\root\cimv2", wmiConnectionOptions)
            oq = New ObjectQuery("select * from Win32_MappedLogicalDisk")
            query = New ManagementObjectSearcher(wmiScope, oq)
            queryCollection = query.Get()
            For Each oReturn As ManagementObject In queryCollection
                If oReturn.Properties("ProviderName").Value.ToString() = Nothing Then
                    'userName = "Nobody currently logged on"
                Else
                    mappeddrvs.Add(oReturn.Properties("ProviderName").Value.ToString() & "|" & DirectCast(oReturn.Properties("Caption").Value, String))
                End If
            Next
            Return mappeddrvs
        Catch
            Return mappeddrvs
        End Try


        'If Not strcompname = "" Then
        '    'Try
        '    'Dim scope As New ManagementScope("\\" & strcompname & "\root\cimv2")

        '    Dim searcher As ManagementObjectSearcher
        '    searcher = New ManagementObjectSearcher("\\" & strcompname & "\root\cimv2",
        '                                            "select * from Win32_MappedLogicalDisk")

        '    For Each share In searcher.Get
        '        MessageBox.Show(share.GetPropertyValue("Path"))
        '        'MappedDrives.dgmapped.Rows.Add(New String() {share("Name").ToString(), share("ProviderName").ToString()})
        '    Next share
        '    ' MappedDrives.Visible = True
        '    'MappedDrives.dgmapped.AutoResizeColumns()
        '    'MappedDrives.dgmapped.AutoSize = True
        '    'Catch
        '    'End Try
        'End If
    End Function
End Class
